package com.rdb.mbean;

import com.rdb.entities.*;
import static com.rdb.mbean.UsersController.AUTH_KEY;
import static com.rdb.mbean.UsersController.AUTH_KEY_FULLNAME;
import com.rdb.mbean.util.JsfUtil;
import com.rdb.mbean.util.PaginationHelper;
import com.rdb.sbean.GroupsFacade;
import com.rdb.sbean.MessagesFacade;
import com.rdb.sbean.UsersFacade;

import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean(name = "messagesController")
public class MessagesController implements Serializable {

    private Messages current;
    private DataModel items = null;
    @EJB
    private com.rdb.sbean.MessagesFacade ejbFacade;
    @EJB
    private com.rdb.sbean.UsersFacade ejbUsersFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public MessagesController() {
    }

    public Messages getSelected() {
        if (current == null) {
            current = new Messages();

        }
        return current;
    }
    
    public void setSelected(Messages messages) {
        this.current = messages;
    }

    public UsersFacade getEjbUsersFacade() {
        return ejbUsersFacade;
    }

    public void setEjbUsersFacade(UsersFacade ejbUsersFacade) {
        this.ejbUsersFacade = ejbUsersFacade;
    }

    private MessagesFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {
                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    Users user = null;
                    if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_KEY) != null) {
                        user = ejbUsersFacade.getUserByUsername(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_KEY).toString());
                    }
                    return new ListDataModel(getFacade().getMessages(user.getUserId(), user.getGroupId().getGroupId()));
                }
            };
        }
        return pagination;
    }

    public String prepareList() {
        recreateModel();
        return "List";
    }

    public String prepareList1() {
        Users user = null;
        if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_KEY) != null) {
            user = ejbUsersFacade.getUserByUsername(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(AUTH_KEY).toString());
        }
        items = new ListDataModel(getFacade().getMessages(user.getUserId(), user.getGroupId().getGroupId()));
        return "/faces/admin/messages/List.xhtml";
    }

    public String prepareEdit() {
        update();
        return "List";
    }

    public String update() {
        try {
            current.setStatus(Boolean.TRUE);
            getFacade().edit(current);
            JsfUtil.addSuccessMessage("Message has been read");
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("bundle/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("bundle/Bundle").getString("GroupsDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("bundle/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    @FacesConverter(forClass = Messages.class)
    public static class MesssagesControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            MessagesController controller = (MessagesController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "messagesController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Messages) {
                Messages o = (Messages) object;
                return getStringKey(o.getId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Groups.class.getName());
            }
        }
    }
}
